package com.demo.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.reggie.comm.R;
import com.demo.reggie.entity.User;
import com.demo.reggie.service.UserService;
import com.demo.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送的短信验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            // 生成4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();

            // 调用阿里云提供的短信服务
            // SMSUtils.sendMessage("瑞吉外卖", "", phone, code);

            // 生成验证码存到session
            session.setAttribute(phone, code);
            return R.success("验证码发送成功");
        }
        return R.success("验证码发送失败");
    }

    /**
     * 移动端用户登录
     *
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map) {
        // // 获取手机号
        // String phone = map.get("phone").toString();
        // // 获取验证码
        // String code = map.get("code").toString();
        // // 从session中获取验证码
        // Object codeInSession = session.getAttribute(phone);
        // // 进行验证码比对
        // if (codeInSession != null && codeInSession.equals(code)) {
        //     LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //     queryWrapper.eq(User::getPhone, phone);
        //     User user = userService.getOne(queryWrapper);
        //     if(user != null) {
        //         user = new User();
        //         user.setPhone(phone);
        //         userService.save(user);
        //     }
        //     return R.success(user);
        // }
        // return R.error("登录失败");

        // 获取手机号
        String phone = map.get("phone").toString();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);
        if(user == null) {
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }

        redisTemplate.opsForValue().set("user", user.getId(), 7, TimeUnit.DAYS);
        return R.success(user);
    }

}
